package com.energy.analysis.basic.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.energy.analysis.basic.entity.CollectData;
import com.energy.analysis.basic.entity.StationData;
import com.energy.analysis.basic.mapper.CollectMapper;
import com.energy.analysis.comm.base.BaseMapper;
import com.energy.analysis.comm.base.BaseService;
import com.energy.analysis.comm.utils.DateUtils;
import com.energy.analysis.comm.utils.StringUtils;

@Service
public class CollectService extends BaseService<CollectData>{
	
	@Autowired
	private CollectMapper collectMapper;
	@Autowired
	private StationService stationService;
	
	//map 里面groupType是1就是按station_id 分组, 值为2， 按stand_id分组,
	public List<CollectData> getCollectDate(Map map) {
		return collectMapper.getCollectDate(map);
	}
	
	//通过standId和时间来取数据。用在历史曲线
	public List<CollectData>getCollectDateByStandId(Map map) {
		return collectMapper.getCollectDateByStandId(map);
	}

	@Override
	public BaseMapper<CollectData> getMapper() {
		return collectMapper;
	}
	
	/**返回最大批次号 */
	public Integer getMaxBatchNumber(){
		return collectMapper.getMaxBatchNumber();
	}
	
	/**获取站的实时网络信息 */
	public List<Map<String,Object>> getStationRtNwData(Integer maxBatchNumber){
		List<Map<String,Object>> retList =  collectMapper.getStationRtNwData(maxBatchNumber);
		//获取所有站的信息
		List<StationData> listStation = stationService.getStationData();
		//组装数据
		for (Map<String,Object> map : retList) {
			Integer batchNumber = (int)map.get("batch_number");
			String batchNumberString = "";
			if(batchNumber!=null){
				batchNumberString = "20" +  batchNumber.toString()+"00";
			}
			map.put("dtime",DateUtils.transferDtTm(batchNumberString));
			for (StationData stationData : listStation) {
				String stationId = (String)map.get("station_id");
				if(StringUtils.isNotEmpty(stationId) && stationId.equals(stationData.getStationId())){
					map.put("station_name", stationData.getStationName());
					map.put("coordinate", stationData.getCoordinate());
				}
			}
		}
		return retList;
	}
}
